import {
	ref,
	computed
} from 'vue'
import {
	defineStore
} from 'pinia'
// 全局状态库
// export const useUserStore = defineStore('user', () => {

//    const userInfo = ref({})
//       const isLogin = ref(false);//  全局变量 标记 是否登陆了


//    const setUserInfo  = (data)=>{
//    	   userInfo.value = data
//    }

//    const setLoginStatus  = (data)=>{
// 	   isLogin.value = data
//    }
//    const logout = ()=>{
// 	   localStorage.removeItem('token')
// 	   localStorage.removeItem('tokenExpired')
// 	   setLoginStatus(false) //  登陆状态
// 	   setUserInfo({}) //  
//    }
//   return { userInfo,setUserInfo,isLogin,setLoginStatus,logout }
// })

export const useUserStore = defineStore('user', {
	// 数据放这 
	state: () => ({
		userInfo: {},
		token: null,
		tokenExpired: 0
	}),
	getters: {
		isLogin: (state) => !!state.userInfo._id // 相当于computed  计算属性
	},
	// 方法  
	actions: {
		setUserInfo(data) {
			this.userInfo = data
		},
		setToken(data) {
			this.token = data
		},
		setTokenExpired(data) {
			this.tokenExpired = data
		},
		logout() {
			localStorage.removeItem('token')
			localStorage.removeItem('tokenExpired')
			this.setUserInfo({})
			this.setToken(null)
			this.setTokenExpired(0)
		}
	},

	persist: {
		enabled: true,
		strategies: [{
				paths: ['token', 'tokenExpired'],
				storage: localStorage,
			},
			{
				paths: ['userInfo'],
				storage: sessionStorage,
			},
		],
	},

})